package dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Administrator;

import dao.AdministratorDao;
import dao.DaoHelper;

public class AdministratorDaoImp implements AdministratorDao {

	private static AdministratorDaoImp administratorDao = new AdministratorDaoImp();
	private static DaoHelper daoHelper=DaoHelperImp.getBaseDaoInstance();

	private AdministratorDaoImp(){}

	public static AdministratorDaoImp getInstance()
	{
		return administratorDao;
	}

	@Override
	public Administrator find(String name, String password) {
		// TODO Auto-generated method stub
		Connection con=daoHelper.getConnection();
		PreparedStatement stmt=null;
		ResultSet result=null;
		String n = null;
		String p = null;
		Administrator administrator = null;

		try {	
			stmt=con.prepareStatement("select * from administrator where name = ? and password = ?");
			stmt.setString(1, name);
			stmt.setString(2, password);
			result=stmt.executeQuery();
			if (result.next()) {
				n = result.getString(1);
				p = result.getString(2);
				administrator = new Administrator(n, p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closeConnection(con);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeResult(result);
		}
		
		return administrator;

	}

}
